<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="closeDialog"
    @open="openDialog"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>

    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="hAssignRoles">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { assignRoles } from '@/api/employees'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    curId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      list: [], // 可选角色列表
      roleIds: [] // 已选角色列表
    }
  },
  methods: {
    //   关闭弹框
    closeDialog() {
      this.$emit('close-dialog')
    },
    // 获取列表
    async getRoleList() {
      const data = await getRoleList(1, 100)
      this.list = data.rows
      console.log(this.list)
    },
    // 弹框打开执行
    openDialog() {
      this.getRoleList()
      this.hGetUserDetailById()
    },
    // 确定
    async hAssignRoles() {
      await assignRoles({
        id: this.curId,
        roleIds: this.roleIds
      })
      // 关闭弹框
      this.closeDialog()
    },
    // 回显角色
    async hGetUserDetailById() {
      const res = await getUserDetailById(this.curId)
      this.roleIds = res.roleIds
    }

  }
}
</script>
